package com.ning.activitys.skysound.fragment.musicpavilion;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.base.BaseFragment;
import com.ning.activitys.skysound.beans.GsonSinger;
import com.ning.activitys.skysound.datas.SpaceItemDecoration;
import com.ning.activitys.skysound.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class SingerFragment extends BaseFragment{

    private RecyclerView recyclerView;
    private GsonSinger gsonSinger;
    private MainActivity mainActivity;
    private SingerAdapter adapter;
    private ListView listView;
    private ListViewAdapter listViewAdapter;

    private String url = "http://218.200.160.29/rdp2/v5.5/singer_categorys.do?&ua=Android_sst&version=4.2280";


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mp_singer,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_singer_fragment);
        listView = (ListView) view.findViewById(R.id.list_view_singer_type_singer_fragment);
        getData();
    }

     private void getData(){

         VolleySingleton.addRequest(url, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {

                 Gson gson = new Gson();
                 gsonSinger = gson.fromJson(response,GsonSinger.class);

                 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mainActivity);
                 linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                 recyclerView.setLayoutManager(linearLayoutManager);

                 int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.recyclerView_item_dimen);
                 recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));

                 adapter = new SingerAdapter();
                 recyclerView.setAdapter(adapter);

                 listViewAdapter = new ListViewAdapter();
                 listView.setAdapter(listViewAdapter);
             }
         }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {

             }
         });
    }


    /**
     *
     * RecyclerView 适配器
     * */
    private class SingerAdapter extends RecyclerView.Adapter<SingerAdapter.MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(mainActivity).inflate(R.layout.item_singer_singer_fragment,parent,false);
            MyViewHolder myViewHolder = new MyViewHolder(view);

            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            VolleySingleton.getInstance().loadImage( holder.singerImg,gsonSinger.getHots().get(position).getImg());
            holder.singerName.setText(gsonSinger.getHots().get(position).getSinger());
        }

        @Override
        public int getItemCount() {
            return gsonSinger.getHots().size();
        }


        class MyViewHolder extends RecyclerView.ViewHolder{

            private NetworkImageView singerImg;
            private TextView singerName;

             public MyViewHolder(View itemView) {
                 super(itemView);

                 singerImg = (NetworkImageView) itemView.findViewById(R.id.singer_image_singer_fragment);
                 singerName = (TextView) itemView.findViewById(R.id.tv_singer_name_singer_fragment);
             }
         }

    }


     private class ListViewAdapter extends BaseAdapter{

         @Override
         public int getCount() {
             return gsonSinger.getCategories().size();
         }

         @Override
         public Object getItem(int position) {
             return null;
         }

         @Override
         public long getItemId(int position) {
             return position;
         }

         @Override
         public View getView(int position, View convertView, ViewGroup parent) {

             MyViewHolder myViewHolder;
             if (convertView == null){
                 convertView = LayoutInflater.from(mainActivity).inflate(R.layout.item_singer_type_singer_fragment,parent,false);
                 myViewHolder = new MyViewHolder(convertView);
                 convertView.setTag(myViewHolder);
             }else {
                 myViewHolder = (MyViewHolder) convertView.getTag();
             }

             myViewHolder.singerType.setText(gsonSinger.getCategories().get(position).getTitle());
             return convertView;
         }

         private class MyViewHolder{

             private TextView singerType;

             public MyViewHolder(View view){

                 singerType = (TextView) view.findViewById(R.id.tv_singer_type_singer_fragment);
             }
         }
     }


}
